package com.example.tongzhi;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.TaskStackBuilder;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.Icon;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
private  static final  String NOT="hshsgh";
    private static  final  int NOT_NVV=0xf1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
    }
    public void initview(){
        Button button=findViewById(R.id.button);
        Button button1=findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onclick();
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send();
            }
        });
    }
    //传统方式
    private void onclick(){
        NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//        Notification noty=new Notification();
//        noty.tickerText="通知内容";
//        noty.icon=R.mipmap.ic_launcher;
        Notification notification=
             new Notification.Builder(this)
                .setContentTitle("标题")
                .setContentText("内容")
                .setSmallIcon(R.mipmap.ic_launcher).build();

        manager.notify(1,notification);
    }
    private void send(){

        NotificationManager nm=getSystemService(NotificationManager.class);
        //兼容性库创建 NotificationManager
//        NotificationManagerCompat nmc=NotificationManagerCompat.from(this);

        //首先构建信道
        //参数1：信道id
        //参数2：信道名称
        //通知的重要级别
        NotificationChannel channel=new NotificationChannel(NOT,
                "测试信道",NotificationManager.IMPORTANCE_HIGH);
//        nmc.createNotificationChannel(channel);

        //将信道注册到系统的通知服务中
        nm.createNotificationChannel(channel);
        Intent intent=new Intent(this,MainActivity2.class);
//        构造任务栈构造器工具
        TaskStackBuilder tsb=TaskStackBuilder.create(this);
        //添加intent
//        注意 目标activity必须在AndroidManife.xml中的activity中
//        设置parentActivityName属性
        tsb.addNextIntentWithParentStack(intent);
//        通过TaskStackBuilder生成PendingIntent对象
//        交由NotificationBuilder构造通知对象
        PendingIntent pi=tsb.getPendingIntent(NOT_NVV,PendingIntent.FLAG_CANCEL_CURRENT);
        //
        //参数2：requestCode
        //参数3：跳转到目标Activity的Intent
        //参数4：点击后对通知的后续操作
        // PendingIntent.FLAG_CANCEL_CURRENT点击通知跳转后销毁通知
        // PendingIntent.FLAG_UPDATE_CURRENT通知不销毁，实时更新通知内容
//        PendingIntent pi=PendingIntent.getActivity(this,NOT_NVV,intent,
//                PendingIntent.FLAG_CANCEL_CURRENT);
//        Notification notification1= new NotificationCompat.Builder(this,NOT);
      //构造通知体对象
       Notification notification=new Notification.Builder(this,NOT)
                .setContentTitle("信道标题").setContentText("信道内容")
               //设置通知时间
               .setWhen(System.currentTimeMillis())
               //是否显示时间
               .setShowWhen(true)
               //设置大图标
               .setLargeIcon(Icon.createWithResource(this,R.mipmap.ic_launcher_round))
               //设置大图
//               .setStyle(new Notification.BigPictureStyle()
//                       .bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.tupian)))
               .setStyle(new Notification.BigTextStyle().bigText("5544444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444"))
               //设置点击跳转后自动销毁
               .setAutoCancel(true)
               //设置是否常驻通知栏
               .setOngoing(false)
               //添加点击后续操作
               .setContentIntent(pi)
               .setSmallIcon(R.drawable.ic_launcher_background)
               .setColor(Color.parseColor("#ff0000"))
               .build();
         //id的值在每次发送通知时均相同，则通知会覆盖
        //若每次消息的id值不同，则对应通知栏创建新的通知对象
       nm.notify(1,notification);
//       nmc.notify(1,notification1);
    }

}